package com.educhains.web.ctrl.frontend;


import com.educhains.module.blog.service.BlogService;
import com.educhains.module.blog.vo.BlogReply;
import com.educhains.module.user.service.UserService;
import com.educhains.module.user.vo.User;
import com.educhains.web.util.SessionUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class BlogCtrl {

    private static final Logger logger = LogManager.getLogger(DemoCtrl.class);

    @Autowired
    private BlogService blogService = null;

    @Autowired
    private UserService userService = null;


    @RequestMapping(value = "/blogList", method = RequestMethod.GET)
    public String getBlog(HttpServletRequest request) {


        String sortOption = request.getParameter("option") == null ? "1" : request.getParameter("option");
        String pageNum = request.getParameter("p") == null ? "1" : request.getParameter("p");//获取页码，默认1
        request.setAttribute("page", blogService.showByPage(Integer.valueOf(pageNum), Integer.valueOf(sortOption)));
        request.setAttribute("option", sortOption);

        return "frontend/blog/blogList";
    }


    @RequestMapping(value = "/readBlog", method = RequestMethod.GET)
    public String getBlogDetail(HttpServletRequest request) {

        String blogId = request.getParameter("pid");
        String pageId = request.getParameter("p") == null ? "1" : request.getParameter("p");//获取页码，默认1
        request.setAttribute("blog", blogService.findBlog(blogId));
        request.setAttribute("blogContent", blogService.findBlogContent(blogId));
        request.setAttribute("page", blogService.replyShowByPage(Integer.valueOf(pageId),blogId));
        request.setAttribute("isLiked" , Boolean.parseBoolean((String)request.getSession().getAttribute(blogId)));
        return "frontend/blog/readBlog";
    }

    @RequestMapping(value = "/newBlog", method = RequestMethod.GET)
    public String newBlog(HttpServletRequest request) {

        return "frontend/blog/newBlog";
    }


    @RequestMapping(value = "/addBlog", method = RequestMethod.POST)
    @ResponseBody
    public String addBlog(HttpServletRequest request) {

        blogService.addBlog(
                // TODO: 2017/9/3  RANDOM ID
                new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date()),
                request.getParameter("title"),
                SessionUtils.getUser(request).getUserId(),
                request.getParameter("summary"),
                Integer.parseInt(request.getParameter("isOriginal")),
                request.getParameter("content"));

        User sessionUser = SessionUtils.getUser(request);
        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 50);
        return "true";

    }

    @RequestMapping(value = "/addReadNum", method = RequestMethod.POST)
    @ResponseBody
    public String addReadNum(HttpServletRequest request) {

        blogService.addReadNum(
                // TODO: 2017/9/3  RANDOM ID
                request.getParameter("pid"));

        return "true";

    }

    @RequestMapping(value = "/addLike", method = RequestMethod.POST)
    @ResponseBody
    public String addLike(HttpServletRequest request) {
        String pid = request.getParameter("pid");
        request.getSession().setAttribute(pid, "true");
        blogService.addLike(
                // TODO: 2017/9/3  RANDOM ID
                request.getParameter("pid"));

        return "true";
    }

    @RequestMapping(value = "/subtractLike", method = RequestMethod.POST)
    @ResponseBody
    public String subtractLike(HttpServletRequest request) {

        String pid = request.getParameter("pid");
        request.getSession().setAttribute(pid, "false");
        blogService.subtractLike(
                // TODO: 2017/9/3  RANDOM ID
                request.getParameter("pid"));

        return "true";
    }

    @RequestMapping(value = "/addReply", method = RequestMethod.POST)
    @ResponseBody
    public String addReply(HttpServletRequest request) {

        String pid = request.getParameter("pid");
        String content = request.getParameter("content");
        blogService.addReply(
                // TODO: 2017/9/3 RANDOM ID
                new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date()),
                pid,
                SessionUtils.getUser(request).getUserId(),
                content
        );

        User sessionUser = SessionUtils.getUser(request);
        userService.updateContributionByUserEmail(sessionUser.getUserEmail(), 5);


        return "true";

    }

    @RequestMapping(value = "/getReply", method = RequestMethod.POST)
    public String getReply(HttpServletRequest request) {

        String pid = request.getParameter("pid");
        String locationId = request.getParameter("locationId");
        request.setAttribute("blogInFloor",  blogService.getReply(
                // TODO: 2017/9/3 RANDOM ID
                pid,
                locationId
        ));

        return "frontend/blog/readBlogReplyInFloorList";
    }


    @RequestMapping(value = "/submitInFloorReply", method = RequestMethod.POST)
    public String submitInFloorReply(HttpServletRequest request) {

        String pid = request.getParameter("pid");
        String locationId = request.getParameter("locationId");
        String content = request.getParameter("content");
        blogService.submitInFloorReply(
                // TODO: 2017/9/3 RANDOM ID
                new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date()),
                SessionUtils.getUser(request).getUserId(),
                pid,
                locationId,
                content
        );
        request.setAttribute("blogInFloor",  blogService.getReply(
                // TODO: 2017/9/3 RANDOM ID
                pid,
                locationId
        ));

        return "frontend/blog/readBlogReplyInFloorList";
    }
}